package com.ruoyi.websocket;


import com.ruoyi.websocket.WebSocketServer;
import com.ruoyi.websocket.IPUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * WebSocket Controller
 *
 * @author wgb
 * @date 2024/06/06
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/ws")
@RequiredArgsConstructor
public class WebSocketController {

    private final WebSocketServer webSocketServer;

    /**
     * 发送消息到指定用户
     *
     * @param message  消息
     * @return {@link ResponseEntity}<{@link String}>
     */
    @GetMapping("/sendAll")
    public ResponseEntity<String> sendAll(String message) throws IOException {
        webSocketServer.sendAllMessage(message);
        return ResponseEntity.ok("消息发送成功");
    }

    @PostMapping("/send/{userId}")
    public ResponseEntity<String> send(@RequestBody String message, @PathVariable String userId) throws IOException {
        webSocketServer.sendInfo(message, userId);
        return ResponseEntity.ok("消息发送成功");
    }

    /**
     * 群发消息
     *
     * @param message 消息
     * @param request 请求
     * @return {@link ResponseEntity}<{@link String}>
     */
    @GetMapping("/push")
    public ResponseEntity<String> pushToWeb(String message, HttpServletRequest request) {
        return ResponseEntity.ok("群发消息成功");
    }
}
